﻿using System;
using System.Collections.Generic;
using System.Text;
using ElectronicCommerce.Framework.Data.Entity.EntityQueryCondition;
using ElectronicCommerce.Framework.Utility;

namespace ElectronicCommerce.Framework.Data.Utility.EntityQueryConditionBuilder
{
    /// <summary>
    ///  Query Build Helper
    /// </summary>
    public static class QueryBuildHelper
    {
        public static string SqlGetConditionStr(string queryInfo, string filedName)
        {
            Range<string, StringOperator> tempRange = new Range<string, StringOperator>();
            tempRange.Value = queryInfo;
            tempRange.Operator = StringOperator.Equal;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(string queryInfo, StringOperator operatorType, string filedName)
        {
            Range<string, StringOperator> tempRange = new Range<string, StringOperator>();
            tempRange.Value = queryInfo;
            tempRange.Operator = operatorType;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(Range<string, StringOperator> condition, string filedName)
        {
            StringBuilder sbCondition = new StringBuilder();
            if (!string.IsNullOrEmpty(condition.Alias))
            {
                sbCondition.Append(condition.Alias);
                sbCondition.Append(".");
            }

            if (StringOperator.Contains == condition.Operator)
            {
                sbCondition.Append(filedName + " LIKE N'%");
                sbCondition.Append(condition.Value);
                sbCondition.Append("%'");
            }
            else if (StringOperator.Equal == condition.Operator)
            {
                sbCondition.Append(filedName + " = N'");
                sbCondition.Append(SQLReplaceManagement.Replace(condition.Value));
                sbCondition.Append("'");
            }
            else if (StringOperator.UnEqual == condition.Operator)
            {
                sbCondition.Append(filedName + " != N'");
                sbCondition.Append(SQLReplaceManagement.Replace(condition.Value));
                sbCondition.Append("'");
            }
            else if (StringOperator.StartsWith == condition.Operator)
            {
                sbCondition.Append(filedName + " LIKE N'");
                sbCondition.Append(SQLReplaceManagement.Replace(condition.Value));
                sbCondition.Append("%'");
            }
            else if (StringOperator.EndsWith == condition.Operator)
            {
                sbCondition.Append(filedName + " LIKE N'%");
                sbCondition.Append(SQLReplaceManagement.Replace(condition.Value));
                sbCondition.Append("'");
            }
            else if (StringOperator.IsNull == condition.Operator)
            {
                sbCondition.Append(filedName + " IS NULL ");
            }
            else if (StringOperator.IsNotNull == condition.Operator)
            {
                sbCondition.Append(filedName + " IS NOT NULL ");
            }
            else if (StringOperator.In == condition.Operator
                && condition.ListValue != null && condition.ListValue.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var value in condition.ListValue)
                {
                    sb.AppendFormat(" N'{0}' ,", SQLReplaceManagement.Replace(value));
                }
                sbCondition.Append(filedName + " IN (" + sb.ToString().TrimEnd(',') + ") ");
            }
            else
            {
                sbCondition.Append(filedName + " = N'");
                sbCondition.Append(SQLReplaceManagement.Replace(condition.Value));
                sbCondition.Append("'");
            }
            return sbCondition.ToString();
        }

        public static string SqlGetConditionStr(int queryInfo, string filedName)
        {
            Range<int, NumberOperator> tempRange = new Range<int, NumberOperator>();
            tempRange.Value = queryInfo;
            tempRange.Operator = NumberOperator.Equal;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(int queryInfo1, int queryInfo2, string filedName)
        {
            Range<int, NumberOperator> tempRange = new Range<int, NumberOperator>();
            tempRange.Value = queryInfo1;
            tempRange.Start = queryInfo1;
            tempRange.End = queryInfo2;
            tempRange.Operator = NumberOperator.Between;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(int queryInfo, NumberOperator operatorType, string filedName)
        {
            Range<int, NumberOperator> tempRange = new Range<int, NumberOperator>();
            tempRange.Value = queryInfo;
            tempRange.Operator = operatorType;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(int queryInfo1, int queryInfo2, NumberOperator operatorType, string filedName)
        {
            Range<int, NumberOperator> tempRange = new Range<int, NumberOperator>();
            tempRange.Value = queryInfo1;
            tempRange.Start = queryInfo1;
            tempRange.End = queryInfo2;
            tempRange.Operator = operatorType;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(Range<int?, NumberOperator> condition, string filedName)
        {
            if (condition != null)
            {
                Range<int, NumberOperator> c = new Range<int, NumberOperator>();
                if (condition.End.HasValue)
                {
                    c.End = condition.End.Value;
                }
                c.Operator = condition.Operator;
                if (condition.Start.HasValue)
                {
                    c.Start = condition.Start.Value;
                }
                if (condition.Value.HasValue)
                {
                    c.Value = condition.Value.Value;
                }
                if (!string.IsNullOrEmpty(condition.Alias))
                {
                    c.Alias = condition.Alias;
                }

                return SqlGetConditionStr(c, filedName);
            }
            return "  ";
        }

        public static string SqlGetConditionStr(Range<int, NumberOperator> condition, string filedName)
        {
            StringBuilder sbCondition = new StringBuilder();
            if (!string.IsNullOrEmpty(condition.Alias))
            {
                sbCondition.Append(condition.Alias);
                sbCondition.Append(".");
            }

            if (NumberOperator.GT == condition.Operator)
            {
                sbCondition.Append(filedName + " > ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.GTEQ == condition.Operator)
            {
                sbCondition.Append(filedName + " >= ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.LT == condition.Operator)
            {
                sbCondition.Append(filedName + " < ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.LTEQ == condition.Operator)
            {
                sbCondition.Append(filedName + " <= ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.UnEqual == condition.Operator)
            {
                sbCondition.Append(filedName + " != ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.Equal == condition.Operator)
            {
                sbCondition.Append(filedName + " = ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.Between == condition.Operator)
            {
                sbCondition.Append(" ( " + filedName + " >= ");
                sbCondition.Append(condition.Start);
                sbCondition.Append(" And " + filedName + " <= ");
                sbCondition.Append(condition.End + " ) ");
            }
            else if (NumberOperator.IsNull == condition.Operator)
            {
                sbCondition.Append(filedName + " IS NULL ");
            }
            else if (NumberOperator.IsNotNull == condition.Operator)
            {
                sbCondition.Append(filedName + " IS NOT NULL ");
            }
            else if (NumberOperator.Contains == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '%" + condition.Value + "%'");
            }
            else if (NumberOperator.StartsWith == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '" + condition.Value + "%'");
            }
            else if (NumberOperator.EndsWith == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '%" + condition.Value + "'");
            }
            else if (NumberOperator.In == condition.Operator
                && condition.ListValue != null && condition.ListValue.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var value in condition.ListValue)
                {
                    sb.AppendFormat(" {0} ,", value);
                }
                sbCondition.Append(filedName + " IN (" + sb.ToString().TrimEnd(',') + ") ");
            }
            else
            {
                sbCondition.Append(filedName + " = ");
                sbCondition.Append(condition.Value);
            }
            return sbCondition.ToString();

        }

        public static string SqlGetConditionStr(decimal queryInfo, string filedName)
        {
            Range<decimal, NumberOperator> tempRange = new Range<decimal, NumberOperator>();
            tempRange.Value = queryInfo;
            tempRange.Operator = NumberOperator.Equal;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(decimal queryInfo1, decimal queryInfo2, string filedName)
        {
            Range<decimal, NumberOperator> tempRange = new Range<decimal, NumberOperator>();
            tempRange.Value = queryInfo1;
            tempRange.Start = queryInfo1;
            tempRange.End = queryInfo2;
            tempRange.Operator = NumberOperator.Between;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(decimal queryInfo, NumberOperator operatorType, string filedName)
        {
            Range<decimal, NumberOperator> tempRange = new Range<decimal, NumberOperator>();
            tempRange.Value = queryInfo;
            tempRange.Operator = operatorType;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(decimal queryInfo1, decimal queryInfo2, NumberOperator operatorType, string filedName)
        {
            Range<decimal, NumberOperator> tempRange = new Range<decimal, NumberOperator>();
            tempRange.Value = queryInfo1;
            tempRange.Start = queryInfo1;
            tempRange.End = queryInfo2;
            tempRange.Operator = operatorType;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(Range<decimal?, NumberOperator> condition, string filedName)
        {
            if (condition != null)
            {
                Range<decimal, NumberOperator> c = new Range<decimal, NumberOperator>();
                if (condition.End.HasValue)
                {
                    c.End = condition.End.Value;
                }
                c.Operator = condition.Operator;
                if (condition.Start.HasValue)
                {
                    c.Start = condition.Start.Value;
                }
                if (condition.Value.HasValue)
                {
                    c.Value = condition.Value.Value;
                }
                if (!string.IsNullOrEmpty(condition.Alias))
                {
                    c.Alias = condition.Alias;
                }

                return SqlGetConditionStr(c, filedName);
            }
            return "  ";
        }


        public static string SqlGetConditionStr(Range<Int64?, NumberOperator> condition, string filedName)
        {
            if (condition != null)
            {
                Range<Int64, NumberOperator> c = new Range<Int64, NumberOperator>();
                if (condition.End.HasValue)
                {
                    c.End = condition.End.Value;
                }
                c.Operator = condition.Operator;
                if (condition.Start.HasValue)
                {
                    c.Start = condition.Start.Value;
                }
                if (condition.Value.HasValue)
                {
                    c.Value = condition.Value.Value;
                }
                if (!string.IsNullOrEmpty(condition.Alias))
                {
                    c.Alias = condition.Alias;
                }

                return SqlGetConditionStr(c, filedName);
            }
            return "  ";
        }

        public static string SqlGetConditionStr(Range<Int64, NumberOperator> condition, string filedName)
        {
            StringBuilder sbCondition = new StringBuilder();

            string alias = string.Empty;
            if (!string.IsNullOrEmpty(condition.Alias))
            {
                alias = condition.Alias + ".";
            }

            if (NumberOperator.GT == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " > ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.GTEQ == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " >= ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.LT == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " < ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.LTEQ == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " <= ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.UnEqual == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " != ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.Equal == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " = ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.Between == condition.Operator)
            {
                sbCondition.Append(" ( " + alias + filedName + " >= ");
                sbCondition.Append(condition.Start);
                sbCondition.Append(" AND " + alias + filedName + " <= ");
                sbCondition.Append(condition.End + " ) ");
            }
            else if (NumberOperator.IsNull == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " IS NULL ");
            }
            else if (NumberOperator.IsNotNull == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " IS NOT NULL ");
            }
            else if (NumberOperator.Contains == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '%" + condition.Value + "%'");
            }
            else if (NumberOperator.StartsWith == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '" + condition.Value + "%'");
            }
            else if (NumberOperator.EndsWith == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '%" + condition.Value + "'");
            }
            else if (NumberOperator.In == condition.Operator
                && condition.ListValue != null && condition.ListValue.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var value in condition.ListValue)
                {
                    sb.AppendFormat(" {0} ,", value);
                }
                sbCondition.Append(alias + filedName + " IN (" + sb.ToString().TrimEnd(',') + ") ");
            }
            else
            {
                sbCondition.Append(alias + filedName + " = ");
                sbCondition.Append(condition.Value);
            }
            return sbCondition.ToString();

        }


        public static string SqlGetConditionStr(Range<decimal, NumberOperator> condition, string filedName)
        {
            StringBuilder sbCondition = new StringBuilder();

            string alias = string.Empty;
            if (!string.IsNullOrEmpty(condition.Alias))
            {
                alias = condition.Alias + ".";
            }

            if (NumberOperator.GT == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " > ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.GTEQ == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " >= ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.LT == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " < ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.LTEQ == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " <= ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.UnEqual == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " != ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.Equal == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " = ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.Between == condition.Operator)
            {
                sbCondition.Append(" ( " + alias + filedName + " >= ");
                sbCondition.Append(condition.Start);
                sbCondition.Append(" AND " + alias + filedName + " <= ");
                sbCondition.Append(condition.End + " ) ");
            }
            else if (NumberOperator.IsNull == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " IS NULL ");
            }
            else if (NumberOperator.IsNotNull == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " IS NOT NULL ");
            }
            else if (NumberOperator.Contains == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '%" + condition.Value + "%'");
            }
            else if (NumberOperator.StartsWith == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '" + condition.Value + "%'");
            }
            else if (NumberOperator.EndsWith == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '%" + condition.Value + "'");
            }
            else if (NumberOperator.In == condition.Operator
                && condition.ListValue != null && condition.ListValue.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var value in condition.ListValue)
                {
                    sb.AppendFormat(" {0} ,", value);
                }
                sbCondition.Append(alias + filedName + " IN (" + sb.ToString().TrimEnd(',') + ") ");
            }
            else
            {
                sbCondition.Append(alias + filedName + " = ");
                sbCondition.Append(condition.Value);
            }
            return sbCondition.ToString();

        }

        public static string SqlGetConditionStr(double queryInfo, string filedName)
        {
            Range<double, NumberOperator> tempRange = new Range<double, NumberOperator>();
            tempRange.Value = queryInfo;
            tempRange.Operator = NumberOperator.Equal;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(double queryInfo1, double queryInfo2, string filedName)
        {
            Range<double, NumberOperator> tempRange = new Range<double, NumberOperator>();
            tempRange.Value = queryInfo1;
            tempRange.Start = queryInfo1;
            tempRange.End = queryInfo2;
            tempRange.Operator = NumberOperator.Between;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(double queryInfo, NumberOperator operatorType, string filedName)
        {
            Range<double, NumberOperator> tempRange = new Range<double, NumberOperator>();
            tempRange.Value = queryInfo;
            tempRange.Operator = operatorType;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(double queryInfo1, double queryInfo2, NumberOperator operatorType, string filedName)
        {
            Range<double, NumberOperator> tempRange = new Range<double, NumberOperator>();
            tempRange.Value = queryInfo1;
            tempRange.Start = queryInfo1;
            tempRange.End = queryInfo2;
            tempRange.Operator = operatorType;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(Range<double, NumberOperator> condition, string filedName)
        {
            StringBuilder sbCondition = new StringBuilder();

            string alias = string.Empty;
            if (!string.IsNullOrEmpty(condition.Alias))
            {
                alias = condition.Alias + ".";
            }

            if (NumberOperator.GT == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " > ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.GTEQ == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " >= ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.LT == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " < ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.LTEQ == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " <= ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.UnEqual == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " != ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.Equal == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " = ");
                sbCondition.Append(condition.Value);
            }
            else if (NumberOperator.Between == condition.Operator)
            {
                sbCondition.Append(" ( " + alias + filedName + " >= ");
                sbCondition.Append(condition.Start);
                sbCondition.Append(" AND " + alias + filedName + " <= ");
                sbCondition.Append(condition.End + " ) ");
            }
            else if (NumberOperator.IsNull == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " IS NULL ");
            }
            else if (NumberOperator.IsNotNull == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " IS NOT NULL ");
            }
            else if (NumberOperator.Contains == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '%" + condition.Value + "%'");
            }
            else if (NumberOperator.StartsWith == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '" + condition.Value + "%'");
            }
            else if (NumberOperator.EndsWith == condition.Operator)
            {
                sbCondition.Append(" CAST(" + filedName + " AS VARCHAR(100)) LIKE '%" + condition.Value + "'");
            }
            else if (NumberOperator.In == condition.Operator
                && condition.ListValue != null && condition.ListValue.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var value in condition.ListValue)
                {
                    sb.AppendFormat(" {0} ,", value);
                }
                sbCondition.Append(alias + filedName + " IN (" + sb.ToString().TrimEnd(',') + ") ");
            }
            else
            {
                sbCondition.Append(alias + filedName + " = ");
                sbCondition.Append(condition.Value);
            }
            return sbCondition.ToString();

        }

        public static string SqlGetConditionStr(DateTime queryInfo, string filedName)
        {
            Range<DateTime, DateTimeOperator> tempRange = new Range<DateTime, DateTimeOperator>();
            tempRange.Value = queryInfo;
            tempRange.Operator = DateTimeOperator.Equal;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(DateTime queryInfo1, DateTime queryInfo2, string filedName)
        {
            Range<DateTime, DateTimeOperator> tempRange = new Range<DateTime, DateTimeOperator>();
            tempRange.Value = queryInfo1;
            tempRange.Start = queryInfo1;
            tempRange.End = queryInfo2;
            tempRange.Operator = DateTimeOperator.Between;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(DateTime queryInfo, DateTimeOperator operatorType, string filedName)
        {
            Range<DateTime, DateTimeOperator> tempRange = new Range<DateTime, DateTimeOperator>();
            tempRange.Value = queryInfo;
            tempRange.Operator = operatorType;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(DateTime queryInfo1, DateTime queryInfo2, DateTimeOperator operatorType, string filedName)
        {
            Range<DateTime, DateTimeOperator> tempRange = new Range<DateTime, DateTimeOperator>();
            tempRange.Value = queryInfo1;
            tempRange.Start = queryInfo1;
            tempRange.End = queryInfo2;
            tempRange.Operator = operatorType;

            return SqlGetConditionStr(tempRange, filedName);
        }

        public static string SqlGetConditionStr(Range<DateTime?, DateTimeOperator> condition, string filedName)
        {
            if (condition != null)
            {
                Range<DateTime, DateTimeOperator> c = new Range<DateTime, DateTimeOperator>();
                if (condition.End.HasValue)
                {
                    c.End = condition.End.Value;
                }
                c.Operator = condition.Operator;
                if (condition.Start.HasValue)
                {
                    c.Start = condition.Start.Value;
                }
                if (condition.Value.HasValue)
                {
                    c.Value = condition.Value.Value;
                }
                if (!string.IsNullOrEmpty(condition.Alias))
                {
                    c.Alias = condition.Alias;
                }

                return SqlGetConditionStr(c, filedName);
            }
            return "  ";
        }

        public static string SqlGetConditionStr(Range<DateTime, DateTimeOperator> condition, string filedName)
        {
            StringBuilder sbCondition = new StringBuilder();

            string alias = string.Empty;
            if (!string.IsNullOrEmpty(condition.Alias))
            {
                alias = condition.Alias + ".";
            }

            if (DateTimeOperator.GT == condition.Operator)
            {
                if (condition.Value != DateTime.MinValue)
                {
                    sbCondition.Append(alias + filedName + " > '");
                    sbCondition.Append(condition.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                    sbCondition.Append("'");
                }
            }
            else if (DateTimeOperator.GTEQ == condition.Operator)
            {
                if (condition.Value != DateTime.MinValue)
                {
                    sbCondition.Append(alias + filedName + " >= '");
                    sbCondition.Append(condition.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                    sbCondition.Append("'");
                }
            }
            else if (DateTimeOperator.LT == condition.Operator)
            {
                if (condition.Value != DateTime.MinValue)
                {
                    sbCondition.Append(alias + filedName + " < '");
                    sbCondition.Append(condition.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                    sbCondition.Append("'");
                }
            }
            else if (DateTimeOperator.LTEQ == condition.Operator)
            {
                if (condition.Value != DateTime.MinValue)
                {
                    sbCondition.Append(alias + filedName + " <= '");
                    sbCondition.Append(condition.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                    sbCondition.Append("'");
                }
            }
            else if (DateTimeOperator.UnEqual == condition.Operator)
            {
                if (condition.Value != DateTime.MinValue)
                {
                    sbCondition.Append(alias + filedName + " <> '");
                    sbCondition.Append(condition.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                    sbCondition.Append("'");
                }
            }
            else if (DateTimeOperator.Equal == condition.Operator)
            {
                if (condition.Value != DateTime.MinValue)
                {
                    sbCondition.Append(alias + filedName + " = '");
                    sbCondition.Append(condition.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                    sbCondition.Append("'");
                }
            }
            else if (DateTimeOperator.Between == condition.Operator)
            {
                if (condition.Start != DateTime.MinValue && condition.End != DateTime.MinValue)
                {
                    sbCondition.Append(" ( " + alias + filedName + " >= '");
                    sbCondition.Append(condition.Start.ToString("yyyy-MM-dd HH:mm:ss"));
                    sbCondition.Append("'");
                    sbCondition.Append(" AND " + alias + filedName + " <= '");
                    sbCondition.Append(condition.End.ToString("yyyy-MM-dd HH:mm:ss") + "' ) ");
                }
            }
            else if (DateTimeOperator.IsNull == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " IS NULL ");
            }
            else if (DateTimeOperator.IsNotNull == condition.Operator)
            {
                sbCondition.Append(alias + filedName + " IS NOT NULL ");
            }
            else if (DateTimeOperator.In == condition.Operator
                && condition.ListValue != null && condition.ListValue.Count > 0)
            {
                StringBuilder sb = new StringBuilder();
                foreach (var value in condition.ListValue)
                {
                    if (value != DateTime.MinValue)
                    {
                        sb.AppendFormat(" '{0}' ,", value.ToString("yyyy-MM-dd HH:mm:ss"));
                    }
                }
                sbCondition.Append(alias + filedName + " IN (" + sb.ToString().TrimEnd(',') + ") ");
            }
            else
            {
                sbCondition.Append(alias + filedName + " = '");
                sbCondition.Append(condition.Value.ToString("yyyy-MM-dd HH:mm:ss"));
                sbCondition.Append("'");
            }
            return sbCondition.ToString();

        }
    }
}
